/**
 * Title:        Utility Class Library<p>
 * Description:  Some useful utility classes.<p>
 */
package com.timber.util;


import java.awt.*;
import javax.swing.*;
import pv.jfcx.*;
import java.text.*;
import java.math.*;
/**
 *	class extending SLNumberRenderer.
 *	used to display negative values with red foreground and in ()
 *
 */
public class SLCurrencyRenderer extends DefaultSLObjectRenderer
{

  protected Color	negFg	= Color.RED;

  public SLCurrencyRenderer()
  {
    this( 12, 0 );
  }

  public SLCurrencyRenderer( int integerPlaces, int decimalPlaces )
  {
        this( integerPlaces, decimalPlaces, SwingConstants.RIGHT );
  }

  public SLCurrencyRenderer( int integerPlaces, int decimalPlaces, int alignment )
  {
    super(JPVTable.DOUBLE);

    NumberFormat nf = new DecimalFormat("#,##0.00;(#,##0.00)");;
    nf.setMaximumIntegerDigits( integerPlaces );
    nf.setMaximumFractionDigits( decimalPlaces );
    nf.setMinimumFractionDigits( decimalPlaces );
    //nf.setMinimumFractionDigits( 0 );   // display decimal places only to what data requires

    m_format = nf;

    setHorizontalAlignment( alignment );

  }

  /**
   *	set foreground RED for negative values
   */
  public void setProperties(SLTableModel tm, int row, int col, Object o)
  {

    super.setColorsAndFont(tm, row, col);

    if( negFg != null && o instanceof Number )
      if( ( o instanceof Long &&
	    ((Long)o).longValue() < 0 )
	  || ( o instanceof Integer && ((Integer)o).intValue() < 0 )
	  || ( o instanceof Double && ((Double)o).doubleValue() < 0 )
	  || ( o instanceof BigDecimal && ((BigDecimal)o).doubleValue() < 0 )
	  )
	setForeground( negFg );
  }

  /**
   * set negative foreground color.
   * set to <i>null<\i> to use default foreground
   */
  public void setNegativeForeground( Color fg ) { negFg = fg; }

}
